package main

import (
	"fmt"
	"time"
)

type Config struct {
	Server   ServerConfig   `json:"server"`
	Registry RegistryConfig `json:"registry"`
	Logging  LoggingConfig  `json:"logging"`
}

type ServerConfig struct {
	Port         string        `json:"port"`
	HttpSPort    string        `json:"https_port"`
	TlsCert      string        `json:"tls_cert"`
	TlsKey       string        `json:"tls_key"`
	ReadTimeout  time.Duration `json:"read_timeout"`
	WriteTimeout time.Duration `json:"write_timeout"`
	IdleTimeout  time.Duration `json:"idle_timeout"`
}

type RegistryConfig struct {
	Upstream   string            `json:"upstream"`
	Headers    map[string]string `json:"headers"`
	RetryCount int               `json:"retry_count"`
	RetryDelay time.Duration     `json:"retry_delay"`
}

type LoggingConfig struct {
	Level  string `json:"level"`
	Format string `json:"format"`
}

func DefaultConfig() *Config {
	return &Config{
		Server: ServerConfig{
			Port:         "80",
			ReadTimeout:  30 * time.Second,
			WriteTimeout: 30 * time.Second,
			IdleTimeout:  60 * time.Second,
		},
		Registry: RegistryConfig{
			Upstream:   "https://registry-1.docker.io",
			Headers:    make(map[string]string),
			RetryCount: 3,
			RetryDelay: 1 * time.Second,
		},
		Logging: LoggingConfig{
			Level:  "info",
			Format: "text",
		},
	}
}

func (c *Config) Validate() error {
	if c.Server.Port == "" {
		return fmt.Errorf("server port cannot be empty")
	}

	if c.Registry.Upstream == "" {
		return fmt.Errorf("registry upstream cannot be empty")
	}

	return nil
}
